package com.monika.main.base;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.PropertiesPropertySourceLoader;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginTrackedValue;
import org.springframework.core.env.*;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EnvPropertyProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {


        MutablePropertySources sources = environment.getPropertySources();

        for (PropertySource<?> source : sources) {


            if (source instanceof MapPropertySource) {
                MapPropertySource map = (MapPropertySource) source;



                String[] names = map.getPropertyNames();
                for (String key : names) {
                    Object property = map.getProperty(key);
                    if (property instanceof String) {
                        String val = String.valueOf(property);
                        if (val.startsWith("CUTE@")) {
                            Map<String, Object> mm = map.getSource();
                            Map<String,Object> mv=new HashMap<>();
                            mv.putAll(mm);
                            OriginTrackedValue o = (OriginTrackedValue) mm.get(key);
                            Origin origin = o.getOrigin();
                            mv.put(key,  OriginTrackedValue.of(val.replace("CUTE@", ""),origin));//val.replace("CUTE@", "")
                            MapPropertySource news=new MapPropertySource(map.getName(),mv);
                            sources.replace(map.getName(),news);
                        }
                    }
                }


            }

        }


    }
}


